/*
 *  Copyright 2011.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.pc.ui.util;

import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.opu.pc.util.IOSafeUtils;

/**
 *  Content of delfault resources 
 */
public class ResourceHandle {
    /** Path to resources */
    public static final String RESOURSE_DIR = "/org/opu/pc/ui/resource/";

    /** Application properties file name */
    public static final String RESOURSE_PATH = RESOURSE_DIR + "Appliction.properties";

    /** Path to small icons */
    public static final String SMALL_ICONS = RESOURSE_DIR + "icons/small/";

    /** Path to medium icons */
    public static final String MEDIUM_ICONS = RESOURSE_DIR + "icons/medium/";

    /** Path to big icons */
    public static final String LARGE_ICONS = RESOURSE_DIR + "icons/large/";

    public static final String APPLICATION_NAME = "Application.name";
    public static final String APPLICATION_TITLE = "Application.title";
    public static final String APPLICATION_VERSION = "Application.version";
    public static final String APPLICATION_VERSION_MINOR = "Application.version.minor";
    public static final String APPLICATION_VENDOR = "Application.vendor";

    /** Icon types */
    public static enum IconType {
        /** SMALL_ICONS */
        small,
        /** MEDIUM_ICONS */
        medium,
        /** LARGE_ICONS */
        large;
    }

    /** Application properties */
    private static final Properties properties = new Properties();

    /** Map with icons, for cashing data */
    private static Map<String, Icon> icons = new HashMap<String, Icon>();

    static {//load application properties
        try {
            properties.load(IOSafeUtils.getResourceAsStream(RESOURSE_PATH));
        } catch (Exception ex) {
            throw new Error("fatal error on load properties: " + RESOURSE_PATH);
        }
    }

    /**
     *  Init properties with data from current jar file
     * @param properties initing properties, if it null creating new instance
     * @param path path to resource
     * @return properties with data
     */
    public static Properties loadProperties(Properties properties, String path){
        if (properties == null){
            properties = new Properties();
        }

        try {
            properties.load(IOSafeUtils.getResourceAsStream(path));
        } catch (IOException ex) {
            Logger.getInstance().error(ResourceHandle.class, "Now data: " + path);
            Logger.getInstance().error(ex);
        }

        return properties;
    }

    /**
     * Return small icon from jar file
     * @param key key icon name
     * @return small icon from jar file
     */
    public static Icon getIcon(String key) {
        return getIcon(key, IconType.small);
    }

    /**
     *  Return icon from jar file
     * @param key icon name
     * @param iconType icon type
     * @return  icon from jar file
     */
    public static Icon getIcon(String key, IconType iconType) {

        String path = MEDIUM_ICONS + key;
        switch (iconType){
            case medium:
                path = MEDIUM_ICONS + key;
                break;
            case large:
                path = LARGE_ICONS + key;
                break;
            case small:
            default:
                path = SMALL_ICONS + key;
                break;
        }

        Icon get = icons.get(path);
        if (get != null){
            return get;
        }

        URL resource = ResourceHandle.class.getResource(path);
        if (resource == null){
            Logger.getInstance().info(ResourceHandle.class, "Now URL '" + path + "'");
            return null;
        }

        get = new ImageIcon(resource);
        icons.put(path, get);

        return get;
    }

    
    public static Image getImage(String key, IconType type){
        ImageIcon icon = (ImageIcon) getIcon(key, type);
        if (icon != null){
            return icon.getImage();
        }
        return null;
    }

    public static Image getImage(String key){
        return getImage(key, IconType.small);
    }

    /**
     *  Return properie value by key, if it`s null
     * returned "${key}"
     * @param key key
     * @return value of application property
     */
    public static String getProperty(String key) {
        return properties.getProperty(key, key);
    }

    public static String getText(String path){
        return getCleanText(RESOURSE_DIR + path);
    }

    /**
     *  Return all data from input file
     * @param fullPath full path to file in jar
     * @return all data from input file 
     */
    public static String getCleanText(String fullPath){
        try {
            return IOSafeUtils.toString(IOSafeUtils.getResourceAsStream(fullPath));
        } catch (Exception ex) {
            Logger.getInstance().error("Can't return \"" + fullPath + "\"");
            Logger.getInstance().error(ex);
            return null;
        }
    }

    /**
     * The same {@link #getProperty(String) } with parametr {@link #APPLICATION_NAME}
     * @return {@link #getProperty(String) } with parametr {@link #APPLICATION_NAME}
     */
    public static String getApplicationName(){
        return properties.getProperty(APPLICATION_NAME);
    }

    /**
     * The same {@link #getProperty(String) } with parametr {@link #APPLICATION_TITLE}
     * @return {@link #getProperty(String) } with parametr {@link #APPLICATION_TITLE}
     */
    public static String getApplicationTitle(){
        return properties.getProperty(APPLICATION_TITLE) + " "
                + properties.getProperty(APPLICATION_VERSION) + "."
                + properties.getProperty(APPLICATION_VERSION_MINOR);
    }

    /**
     * The same {@link #getProperty(String) } with parametr {@link #APPLICATION_VERSION}
     * @return {@link #getProperty(String) } with parametr {@link #APPLICATION_VERSION}
     */
    public static String getApplicationVersion(){
        return properties.getProperty(APPLICATION_VERSION);
    }

    /**
     * @return minor and major string
     */
    public static String getApplicationVersionString(){
        return getApplicationVersion() + "." +
                properties.getProperty(APPLICATION_VERSION_MINOR);
    }

    /**
     * The same {@link #getProperty(String) } with parametr {@link #APPLICATION_VENDOR}
     * @return {@link #getProperty(String) } with parametr {@link #APPLICATION_VENDOR}
     */
    public static String getApplicationVendor(){
        return properties.getProperty(APPLICATION_VENDOR);
    }

    public static Image getWindowImage(){
        return getImage("Main.png", ResourceHandle.IconType.large);
    }

}
